package com.mayikt.weixin.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @Auther: CHEN JIN LONG
 * @Date: 2019/1/15 20:46
 * @Description:
 */
public class FixedSizeThreadPool {

    //线程池仓库
    private BlockingDeque<Runnable> blockingDeque;

    //工作线程集合
    private List<Thread> workers;

    //码农干活
    public static class Worker extends Thread {

        private FixedSizeThreadPool pool;

        public Worker(FixedSizeThreadPool pool) {
            this.pool = pool;
        }

        @Override
        public void run() {
            //去仓库拿东西
            while (this.pool.isWorking || this.pool.blockingDeque.size() > 0) {
                Runnable task = null;

                try {

                    if(this.pool.isWorking){
                        task = this.pool.blockingDeque.take();
                    }else{

                        task = this.pool.blockingDeque.poll();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (task != null) {

                    task.run();
                    System.out.println("线程：" + Thread.currentThread().getName() + "执行完毕...");
                }

            }
        }


    }

    public FixedSizeThreadPool(int poolSize, int taskSize) {

        if (poolSize <= 0 || taskSize <= 0) {

            throw new IllegalArgumentException("非法参数");
        }

        this.blockingDeque = new LinkedBlockingDeque<>(taskSize);
        this.workers = Collections.synchronizedList(new ArrayList());

        for (int i = 0; i < poolSize; i++) {

            Worker worker = new Worker(this);
            worker.start();
            workers.add(worker);
        }


    }

    //不阻塞
    public boolean submit(Runnable task) {
        if (!this.isWorking) {
            return false;
        }
        return this.blockingDeque.offer(task);
    }

    //阻塞
    public void excute(Runnable task) {
        if (!this.isWorking) {
            return;
        }
        try {
            this.blockingDeque.put(task);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private volatile boolean isWorking = true;

    private void shutDown() {
        this.isWorking = false;


    }


    public static void main(String[] args) {
        FixedSizeThreadPool fixedSizeThreadPool = new FixedSizeThreadPool(3, 6);

        for (int i = 0; i < 6; i++) {

            fixedSizeThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("这是一个线程放入仓库...");

                    try {
                        Thread.sleep(2000);
                        System.out.println("一个线程被唤醒...");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            });
        }
        fixedSizeThreadPool.shutDown();
    }
}
